package leetcode.code0445;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;
import leetcode.helper.tree.ListNode;
//执行用时：2 ms, 在所有 Java 提交中击败了88.28%的用户
//
//内存消耗：38.4 MB, 在所有 Java 提交中击败了87.93%的用户
//
//通过测试用例：1563 / 1563
public class Solution implements IDebug {

	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		ListNode[] arr = new ListNode[100];
		ListNode[] brr = new ListNode[100];
		int pa = 0, pb = 0;
		while (l1 != null) {
			arr[pa++] = l1;
			l1 = l1.next;
		}
		while (l2 != null) {
			brr[pb++] = l2;
			l2 = l2.next;
		}
		int carry = 0;
		ListNode ans = new ListNode();
		while (pa > 0 || pb > 0 || carry > 0) {// carry>0 容易丢
			int cur = 0;
			cur += pa > 0 ? arr[--pa].val : 0;
			cur += pb > 0 ? brr[--pb].val : 0;
			cur += carry;
			carry = cur / 10;
			cur = cur % 10;
			ListNode ln = new ListNode(cur);
			ln.next = ans.next;
			ans.next = ln;
		}
		return ans.next;
	}

	@Override
	public void debug1() {
		ListNode e = ListNode.set(HelpDebug.str2array3("[1,0]"));
		ListNode e1 = ListNode.set(HelpDebug.str2array3("[5]"));
		ListNode e2 = ListNode.set(HelpDebug.str2array3("[5]"));
		HelpDebug.compare(e, this.addTwoNumbers(e1, e2));

	}

	@Override
	public void debug2() {
		ListNode e = ListNode.set(HelpDebug.str2array3("[7,8,0,7]"));
		ListNode e1 = ListNode.set(HelpDebug.str2array3("[7,2,4,3]"));
		ListNode e2 = ListNode.set(HelpDebug.str2array3("[5,6,4]"));
		HelpDebug.compare(e, this.addTwoNumbers(e1, e2));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
